package com.service.impl;

import com.mapper.DataBeanDao;
import com.mapper.JobDao;
import com.mapper.JobLogDao;
import com.model.DataBean;
import com.model.Job;
import com.model.JobLog;
import com.service.IJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("jobService")
public class JobServiceImpl implements IJobService {


    @Autowired
    private DataBeanDao dataBeanDao;

    @Autowired
    private JobDao jobDao;

    @Autowired
    private JobLogDao jobLogDao;


    @Override
    public List<DataBean> getSysList() {

        return dataBeanDao.getJobSysList();
    }

    @Override
    public List<DataBean> getTypeList() {
        return dataBeanDao.getJobTypeList();
    }

    @Override
    public List<DataBean> getStatusList() {
        return dataBeanDao.getJobStatusList();
    }

    @Override
    public List<DataBean> getUserList() {
        return dataBeanDao.getUserList();
    }

    /**
     * 获取数据总数
     * @param job
     * @return
     */
    @Override
    public List<Job> getJobList(Job job) {

        int pageNo = job.getPageNo();
        job.setPageDatasBegin(((pageNo-1)*job.getPageSize()));
        job.setPageDatasEnd((pageNo*job.getPageSize()));
        return jobDao.getJobList(job);
    }

    /**
     * 获取数据总量
     * @param job
     * @return
     */
    @Override
    public Integer getJobListSize(Job job) {

        int resultSize = 0;
        List<Map> result = jobDao.getJobListSize(job);
        if(null != result && result.size()==1){
            Object cunObj = result.get(0).get("CUN");
            if(null != cunObj){
                resultSize = Integer.parseInt(cunObj.toString());
            }
        }
        return resultSize;
    }

    @Override
    public String addJob(Job job) {

        try {
            Date now = new Date();
            job.setModifyTime(now);
            if(null == job.getId()){
                job.setCreateTime(now);
                jobDao.addJob(job);
            }else{
                // bean中含有主键为 修改操作
                jobDao.updateJob(job);
            }
        }catch (Exception e){
            e.printStackTrace();
            return "ERR";
        }
        return "OK";
    }

    @Override
    public String deleteJob(Long jobId) {

        try {
            Job job = new Job();
            job.setId(jobId);
            job.setModifyTime(new Date());
            job.setDelStatus(1);
            //jobDao.deleteJobById(jobId);
            jobDao.updateJob(job);
        }catch (Exception e){
            e.printStackTrace();
            return "ERR";
        }
        return "OK";
    }

    @Override
    public Job getJobById(Long id) {
        return jobDao.getJobById(id);
    }

    @Override
    public List<JobLog> getJobLogList(Long jobId) {
        return jobLogDao.getJobLogList(jobId);
    }

    @Override
    public String addJobLog(JobLog jobLog) {

        try {
            jobLog.setAddTime(new Date());
            jobLogDao.addJobLog(jobLog);
        }catch (Exception e){
            e.printStackTrace();
            return "ERR";
        }
        return "OK";
    }
}
